//
//  CompanyViewController.m
//  MedicineBox
//
//  Created by dllo on 15/8/24.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "CompanyViewController.h"
#import "MyCollectionViewCell.h"
#import "DrugListViewController.h"

#import "DKNightVersion.h"
#import "UIImageView+NightImage.h"
@interface CompanyViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@end

@implementation CompanyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.nightBackgroundColor = UIColorFromRGB(0x343434);
    [self createNavigation];
    [self createCollectionView];
}

- (void)viewDidAppear:(BOOL)animated
{
    if (self.companyArray.count == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"没有网络" message:@"请连接网络" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        [alert addAction:ok];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

#pragma mark - 
#pragma mark 设置导航栏
- (void)createNavigation
{
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    navView .backgroundColor = [UIColor colorWithRed:86/255.0 green:137/255.0 blue:156/255.0 alpha:1];
    navView.nightBackgroundColor = UIColorFromRGB(0x444444);
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 63, self.view.frame.size.width, 0.5)];
    line.backgroundColor = [UIColor whiteColor];
    [navView addSubview:line];
    [self.view addSubview:navView];
    [navView release];

    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100) / 2, 30, 100, 30)];
    titleLabel.text = @"合作品牌";
    titleLabel.font = [UIFont fontWithName:@"Baskerville-SemiBold" size:20];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.nightTextColor = [UIColor whiteColor];
    [navView addSubview:titleLabel];
    [titleLabel release];
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10 , 30, 60, 30)];
    [navView addSubview:backButton];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton release];
}

- (void)buttonAction:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - 
#pragma mark 创建collectView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 20;
    UICollectionView *collect = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 70, self.view.frame.size.width - 40, self.view.frame.size.height - 64) collectionViewLayout:layout];
    collect.backgroundColor = [UIColor clearColor];
    collect.delegate = self;
    collect.dataSource = self;
    
    collect.nightBackgroundColor = UIColorFromRGB(0x343434);
    
    
    //注册重用池中所放置的cell类型
    [collect registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:collect];
    [collect release];
    [layout release];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.companyArray.count;
}
#pragma mark -
#pragma mark 自定义cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell valueWithModel:[self.companyArray objectAtIndex:indexPath.item]];
    return cell;
}

//设定每个item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake(60, 90);
    
}
#pragma mark -
#pragma mark 每个item的事件

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *word = @[@"%E6%AD%A5%E9%95%BF",@"%E7%A7%91%E4%BC%A6",@"%E6%81%92%E7%91%9E",@"%E7%BD%97%E6%B0%8F",@"%E4%BA%91%E5%8D%97%E7%99%BD%E8%8D%AF",@"%E8%BE%89%E7%91%9E",@"%E6%8B%9C%E8%80%B3",@"%E8%91%9B%E5%85%B0%E7%B4%A0%E5%8F%B2%E5%85%8B",@"%E9%98%BF%E6%96%AF%E5%88%A9%E5%BA%B7",@"%E5%90%8C%E4%BB%81%E5%A0%82",@"%E8%AF%BA%E5%8D%8E",@"%E6%9D%AD%E5%B7%9E%E9%BB%98%E6%B2%99%E4%B8%9C",@"%E8%B5%9B%E8%AF%BA%E8%8F%B2%E5%AE%89%E4%B8%87%E7%89%B9"];
    
    DrugListViewController *list = [[DrugListViewController alloc] init];
    list.word = [word objectAtIndex:indexPath.item];
    [self presentViewController:list animated:YES completion:nil];
    [list release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
